package de.htwg_konstanz.ebus.framework.webservices;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import de.htwg_konstanz.ebus.framework.webservices.orderservice.ADDRESS;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.BUYERPARTY;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.CONTROLINFO;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDER;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERHEADER;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERINFO;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERITEM;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERPARTIES;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSE;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSEHEADER;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSEINFO;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSEITEM;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSEITEMLIST;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERRESPONSESUMMARY;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.ORDERSUMMARY;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.OrderServiceFault;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.OrderServiceRequest;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.OrderServiceResponse;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.PARTY;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.PARTYID;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.REMARK;
import de.htwg_konstanz.ebus.framework.webservices.orderservice.SUPPLIERPARTY;
import de.htwg_konstanz.ebus.framework.wholesaler.api.bo.BOCustomer;
import de.htwg_konstanz.ebus.framework.wholesaler.api.bo.BOOrderCustomer;
import de.htwg_konstanz.ebus.framework.wholesaler.api.bo.BOOrderItemCustomer;
import de.htwg_konstanz.ebus.framework.wholesaler.api.boa.CustomerBOA;
import de.htwg_konstanz.ebus.framework.wholesaler.api.boa.OrderBOA;
import de.htwg_konstanz.ebus.framework.wholesaler.api.boa.OrderItemBOA;
import de.htwg_konstanz.ebus.framework.wholesaler.api.boa.ProductBOA;

@WebService
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class OrderWebService {
	
	
	@WebMethod
	@WebResult(name = "OrderServiceResponse", targetNamespace = "http://www.htwg-konstanz.de/ebsf/ws/order")
	public OrderServiceResponse performOrder(
			@WebParam(name = "OrderServiceRequest", targetNamespace = "http://www.htwg-konstanz.de/ebsf/ws/order") OrderServiceRequest request) throws OrderServiceFault {

		ORDER requestOrder = request.getORDER();
		ORDERSUMMARY requestOrdersummary = requestOrder.getORDERSUMMARY();
		ORDERHEADER requestOrderheader = requestOrder.getORDERHEADER();
		CONTROLINFO requestControlinfo = requestOrderheader.getCONTROLINFO();
		ORDERINFO requestOrderinfo = requestOrderheader.getORDERINFO();
		ORDERPARTIES requestOrderparties = requestOrderinfo.getORDERPARTIES();
		BUYERPARTY requestBuyerparty = requestOrderparties.getBUYERPARTY();
		PARTY requestBparty = requestBuyerparty.getPARTY();
		ADDRESS requestBaddress = requestBparty.getADDRESS();
		
		List<ORDERITEM> requestOrderitems = requestOrder.getORDERITEMLIST().getORDERITEM();
		
		
		// validate the user, 
		List<BOCustomer> customers = CustomerBOA.getInstance().findAll();
		BOCustomer customer = null;
		if (request.getUserName().equalsIgnoreCase("T12_L1_E1")) {
			customer = customers.get(0);
		} else if (request.getUserName().equalsIgnoreCase("T12_L1_E2")) {
			customer = customers.get(1);
		} else if (request.getUserName().equalsIgnoreCase("T12_L2_E1")) {
			customer = customers.get(2);
		} else if (request.getUserName().equalsIgnoreCase("T12_L2_E2")) {
			customer = customers.get(3);
		} else {
			throw new OrderServiceFault("Unbekannter Kunde",
					"Kunde nicht bekannt");
		}

		
		BOOrderCustomer orderCustomer = new BOOrderCustomer();
		orderCustomer.setCustomer(customer);
		orderCustomer.setDeliveryAddress(customer.getAddress());
		orderCustomer.setInvoiceAddress(customer.getAddress());
		orderCustomer.setOrderDate(new Date());
		orderCustomer.setOrderNumber(request.getORDER().getORDERHEADER().getORDERINFO().getORDERID());

		Long orderNumberCustomer = ((Long) (new Date().getTime() % 10000));
		orderCustomer.setOrderNumberCustomer(orderNumberCustomer.toString());
		BigDecimal priceTotalGross = requestOrdersummary.getTOTALAMOUNT();
		orderCustomer.setPriceTotalGross(priceTotalGross);
		BigDecimal priceTotalNet = new BigDecimal(0);
		orderCustomer.setPriceTotalNet(priceTotalNet);
		BigDecimal taxAmount = new BigDecimal(0);
		orderCustomer.setTaxAmount(taxAmount);
		BigDecimal taxTotal = new BigDecimal(0);
		orderCustomer.setTaxTotal(taxTotal);
		Integer totalLineItems = 0;
		orderCustomer.setTotalLineItems(totalLineItems);

		OrderBOA.getInstance().saveOrUpdate(orderCustomer);

		List<ORDERITEM> items = request.getORDER().getORDERITEMLIST().getORDERITEM();
		for (ORDERITEM item : items) {
			BOOrderItemCustomer orderItem = new BOOrderItemCustomer();
			orderItem.setOrderNumberCustomer(orderNumberCustomer.toString());
			orderItem.setItemNetPrice(item.getARTICLEPRICE().getPRICELINEAMOUNT());
			orderItem.setProductDescription(item.getARTICLEID().getDESCRIPTIONSHORT());
			if (ProductBOA.getInstance().findByShortdescription(
					item.getARTICLEID().getDESCRIPTIONSHORT()) == null) {
				REMARK remark = new REMARK();
				remark.setType("NA");
				remark.setValue("Dieser Artikel ist nicht im Sortiment vorhanden");
				item.getREMARK().add(remark);
				item.setQUANTITY(new BigDecimal(0));
				orderItem.setOrderAmount(0);
			} else {
				orderItem.setOrderAmount(item.getQUANTITY().intValue());
			}
			orderItem.setOrderUnit(item.getORDERUNIT());
			orderItem.setTaxRate(item.getARTICLEPRICE().getTAX().intValue() * 100);
			orderItem.setOrderItemNumber(Integer.parseInt(item.getLINEITEMID()));

			// important! set the order customer to create a relation between
			// the
			// order item and the corresponding order
			orderItem.setOrderCustomer(orderCustomer);
			OrderItemBOA.getInstance().saveOrUpdateOrderItemCustomer(orderItem);
			taxAmount = new BigDecimal(item.getARTICLEPRICE().getTAX().doubleValue() * 100);
		}

		orderCustomer.setTaxTotal(taxAmount);
		orderCustomer.setPriceTotalNet(new BigDecimal((priceTotalGross.doubleValue() / (100 + taxAmount.intValue())) * 100d));
		orderCustomer.setTaxAmount(taxTotal);
		orderCustomer.setTaxAmount(new BigDecimal(orderCustomer.getPriceTotalGross().doubleValue() - orderCustomer.getPriceTotalNet().doubleValue()));
		OrderBOA.getInstance().saveOrUpdate(orderCustomer);

		// the line item count has changed -> update the order customer
		// load all line items to determine the size
		if (items != null) {
			orderCustomer.setTotalLineItems(Integer.valueOf(items.size()));
			OrderBOA.getInstance().saveOrUpdate(orderCustomer);
		}

		// request

		CONTROLINFO controlinfo = new CONTROLINFO();
		controlinfo.setGENERATIONDATE(requestControlinfo.getGENERATIONDATE());
		controlinfo.setGENERATORINFO(requestControlinfo.getGENERATORINFO());
		controlinfo.setSTOPAUTOMATICPROCESSING(requestControlinfo.getSTOPAUTOMATICPROCESSING());

		ORDERRESPONSEHEADER orderresponseheader = new ORDERRESPONSEHEADER();
		orderresponseheader.setCONTROLINFO(controlinfo);

		PARTYID partyid = new PARTYID();
		partyid.setType(requestBparty.getPARTYID().getType());
		partyid.setValue(requestBparty.getPARTYID().getValue());

		ADDRESS address = new ADDRESS();
		address.setBOXNO(requestBaddress.getBOXNO());
		address.setCITY(requestBaddress.getCITY());
		address.setCOUNTRY(requestBaddress.getCOUNTRY());
		address.setDEPARTMENT(requestBaddress.getDEPARTMENT());
		address.setEMAIL(requestBaddress.getEMAIL());
		address.setFAX(requestBaddress.getFAX());
		address.setNAME(requestBaddress.getNAME());

		PARTY party = new PARTY();
		party.setADDRESS(address);
		party.setPARTYID(partyid);

		BUYERPARTY buyerparty = new BUYERPARTY();
		buyerparty.setPARTY(party);

		SUPPLIERPARTY supplierparty = new SUPPLIERPARTY();

		ORDERPARTIES orderparties = new ORDERPARTIES();
		orderparties.setBUYERPARTY(buyerparty);
		orderparties.setSUPPLIERPARTY(supplierparty);

		ORDERRESPONSEINFO orderresponsinfo = new ORDERRESPONSEINFO();
		orderresponsinfo.setORDERDATE(requestOrderinfo.getORDERDATE());
		orderresponsinfo.setORDERID(requestOrderinfo.getORDERID());
		orderresponsinfo.setORDERPARTIES(requestOrderinfo.getORDERPARTIES());
		orderresponsinfo.setORDERRESPONSEDATE(requestOrderinfo.getORDERDATE());
		orderresponsinfo.setPRICECURRENCY(requestOrderinfo.getPRICECURRENCY());
		orderresponsinfo.setSUPPLIERORDERID(((Long) orderNumberCustomer).toString());

		ORDERRESPONSEITEMLIST orderresponseitemlist = new ORDERRESPONSEITEMLIST();

		for (ORDERITEM requestOrderitem : requestOrderitems) {
			// ARTICLEID requestArticleid = requestOrderitem.getARTICLEID();
			// ARTICLEPRICE requestArticleprice = requestOrderitem
			// .getARTICLEPRICE();
			// List<REMARK> requestItemRemark = requestOrderitem.getREMARK();
			ORDERRESPONSEITEM orderresponseitem = new ORDERRESPONSEITEM();
			orderresponseitem.setARTICLEID(requestOrderitem.getARTICLEID());
			orderresponseitem.setARTICLEPRICE(requestOrderitem.getARTICLEPRICE());
			orderresponseitem.setLINEITEMID(requestOrderitem.getLINEITEMID());
			orderresponseitem.setORDERUNIT(requestOrderitem.getORDERUNIT());
			orderresponseitem.setQUANTITY(requestOrderitem.getQUANTITY());
			orderresponseitemlist.getORDERRESPONSEITEM().add(orderresponseitem);
		}
		//		

		ORDERRESPONSESUMMARY orderresponsesummary = new ORDERRESPONSESUMMARY();
		orderresponsesummary.setTOTALAMOUNT(requestOrdersummary.getTOTALAMOUNT());
		orderresponsesummary.setTOTALITEMNUM(requestOrdersummary.getTOTALITEMNUM());

		orderresponseheader.setORDERRESPONSEINFO(orderresponsinfo);
		orderresponseheader.setCONTROLINFO(controlinfo);

		ORDERRESPONSE orderresponse = new ORDERRESPONSE();
		orderresponse.setORDERRESPONSEHEADER(orderresponseheader);
		orderresponse.setORDERRESPONSEITEMLIST(orderresponseitemlist);
		orderresponse.setORDERRESPONSESUMMARY(orderresponsesummary);
		orderresponse.setVersion(request.getORDER().getVersion());

		OrderServiceResponse orderServiceResponse = new OrderServiceResponse();
		orderServiceResponse.setORDERRESPONSE(orderresponse);

		return orderServiceResponse;

	}
}



